﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CarBloke.Controls;
using System.Globalization;

namespace CarBloke.Pages
{
    public partial class VehicleStatsPage : PhoneApplicationPage
    {
        VehicleStats _stats = null;

        public VehicleStatsPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string vehicleID = "";
            if (NavigationContext.QueryString.TryGetValue("vehicleID", out vehicleID))
            {
                Guid id = new Guid(vehicleID);
                Vehicle vehicle = App.LoadVehicle(id);

                if (vehicle != null)
                {
                    _stats = App.LoadVehicleStats(vehicle.ID);
                    buildEfficiencyGraph();
                    //buildCostGraph();
                }
            }
        }

        /// <summary>
        /// Builds the fuel efficiency graph
        /// </summary>
        private void buildEfficiencyGraph()
        {
            Graph graph = new Graph();

            if (_stats != null && _stats.FuelEfficiency != null && _stats.FuelEfficiency.Count > 3)
            {
                //X-Axis labels and values
                var orderedDates = _stats.FuelEfficiency.OrderBy((dv) => dv.Date);
                DateTime startDate = orderedDates.First().Date;
                DateTime endDate = orderedDates.Last().Date;

                DateTime startMonth = new DateTime(startDate.Year, startDate.Month, 1);
                DateTime endMonth = new DateTime(endDate.Year, endDate.Month, 1);
                int totalMonths = 1;
                while (startMonth < endMonth)
                {
                    totalMonths++;
                    startMonth = startMonth.AddMonths(1);
                }

                if (totalMonths == 1)
                {
                    DateTime monthStart = new DateTime(startDate.Year, startDate.Month, 1);         
                    DateTime monthEnd = new DateTime(startDate.Year, startDate.Month, 28);
                    graph.xAxisMarkers.Add(new AxisMarker("", monthStart.Ticks, AxisMarker.TickTypes.None));
                    graph.xAxisMarkers.Add(new AxisMarker(startDate.Month.ToString("d", CultureInfo.CurrentCulture)
                        + "/" + startDate.Year, startDate.Ticks, AxisMarker.TickTypes.None));
                    graph.xAxisMarkers.Add(new AxisMarker("", monthEnd.Ticks, AxisMarker.TickTypes.None));
                }
                else if (totalMonths == 2)
                {
                    //Mark start
                    graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

                    //The month mark
                    DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
                    monthMark = monthMark.AddMonths(1);
                    graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
                        + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));

                    //Mark end 
                    graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));

                }
                else if (totalMonths > 2 && totalMonths < 6)
                {
                    //Mark start
                    graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

                    //The month marks
                    DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
                    monthMark = monthMark.AddMonths(1);
                    while (monthMark < endDate)
                    {
                        //Another way to break out
                        if (monthMark.Year == endDate.Year && monthMark.Month == endDate.Year)
                            break;

                        graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
                            + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));
                        monthMark = monthMark.AddMonths(1);
                    }

                    //Mark end 
                    graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));
                }
                else if (totalMonths >= 6 && totalMonths < 8)
                {
                    //Mark start
                    graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

                    //The month marks
                    DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
                    monthMark = monthMark.AddMonths(1);
                    while (monthMark < endDate)
                    {
                        //Another way to break out
                        if (monthMark.Year == endDate.Year && monthMark.Month == endDate.Year)
                            break;

                        graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
                            + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));
                        monthMark = monthMark.AddMonths(2);
                    }

                    //Mark end 
                    graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));
                }
                else if (totalMonths >= 8)
                {
                    //TODO: Mark start (nl), months at the interval rate (totalMonths % 4), and the end (nl)
                    //Mark start
                    graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

                    //The month marks
                    DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
                    int intervalRate = (int)Math.Floor(totalMonths / 4);
                    monthMark = monthMark.AddMonths(1);
                    while (monthMark < endDate)
                    {
                        //Another way to break out
                        if (monthMark.Year == endDate.Year && monthMark.Month == endDate.Year)
                            break;

                        graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
                            + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));
                        monthMark = monthMark.AddMonths(intervalRate);
                    }

                    //Mark end 
                    graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));
                }


                //Y-Axis labels and values
                var orderedValues = _stats.FuelEfficiency.OrderBy((dv) => dv.Value);
                Double startValue = orderedValues.First().Value;
                Double endValue = orderedValues.Last().Value;
                Double middleValue = startValue + (endValue - startValue) / 2;
                Double buffer = (endValue - startValue) * .35;

                String startValueLabel = UnitConversions.Efficiency(startValue, ApplicationSettings.Current.EfficiencyUnit).ToString("f", CultureInfo.CurrentCulture);
                String middleValueLabel = UnitConversions.Efficiency(middleValue, ApplicationSettings.Current.EfficiencyUnit).ToString("f", CultureInfo.CurrentCulture);
                String endValueLabel = UnitConversions.Efficiency(endValue, ApplicationSettings.Current.EfficiencyUnit).ToString("f", CultureInfo.CurrentCulture);

                graph.yAxisMarkers.Add(new AxisMarker("", startValue - buffer, AxisMarker.TickTypes.None));
                graph.yAxisMarkers.Add(new AxisMarker(startValueLabel, startValue, AxisMarker.TickTypes.Large));
                graph.yAxisMarkers.Add(new AxisMarker(middleValueLabel, middleValue, AxisMarker.TickTypes.Large));
                graph.yAxisMarkers.Add(new AxisMarker(endValueLabel, endValue, AxisMarker.TickTypes.Large));
                graph.yAxisMarkers.Add(new AxisMarker("", endValue + buffer, AxisMarker.TickTypes.None));

                //Add data points
                foreach (DateValue<Double> dateValue in _stats.FuelEfficiency)
                {
                    graph.Data.Add(new OrderedPair<Double, Double>(dateValue.Date.Ticks, dateValue.Value));
                }
            }

            GraphControl graphControl = new GraphControl(graph, ApplicationSettings.ToString(ApplicationSettings.Current.EfficiencyUnit));
            graphControl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            graphControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

            this.efficiencyGrid.Children.Add(graphControl);
        }

        ///// <summary>
        ///// Builds the cost graph
        ///// </summary>
        //private void buildCostGraph()
        //{
        //    Graph graph = new Graph();

        //    if (_stats != null && _stats.CostMonthlyTotal != null && _stats.CostMonthlyTotal.Count > 3)
        //    {
        //        //X-Axis labels and values
        //        var orderedDates = _stats.CostMonthlyTotal.OrderBy((dv) => dv.Date);
        //        DateTime startDate = orderedDates.First().Date;
        //        DateTime endDate = orderedDates.Last().Date;

        //        DateTime startMonth = new DateTime(startDate.Year, startDate.Month, 1);
        //        DateTime endMonth = new DateTime(endDate.Year, endDate.Month, 1);
        //        int totalMonths = 1;
        //        while (startMonth < endMonth)
        //        {
        //            totalMonths++;
        //            startMonth = startMonth.AddMonths(1);
        //        }

        //        if (totalMonths == 1)
        //        {
        //            //TODO: Either no label or mark the month without a notch across the bottom                     
        //        }
        //        else if (totalMonths == 2)
        //        {
        //            //Mark start
        //            graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

        //            //The month mark
        //            DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
        //            monthMark = monthMark.AddMonths(1);
        //            graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
        //                + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));

        //            //Mark end 
        //            graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));

        //        }
        //        else if (totalMonths > 2 && totalMonths < 6)
        //        {
        //            //Mark start
        //            graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

        //            //The month marks
        //            DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
        //            monthMark = monthMark.AddMonths(1);
        //            while (monthMark < endDate)
        //            {
        //                //Another way to break out
        //                if (monthMark.Year == endDate.Year && monthMark.Month == endDate.Year)
        //                    break;

        //                graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
        //                    + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));
        //                monthMark = monthMark.AddMonths(1);
        //            }

        //            //Mark end 
        //            graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));
        //        }
        //        else if (totalMonths >= 6 && totalMonths < 8)
        //        {
        //            //Mark start
        //            graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

        //            //The month marks
        //            DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
        //            monthMark = monthMark.AddMonths(1);
        //            while (monthMark < endDate)
        //            {
        //                //Another way to break out
        //                if (monthMark.Year == endDate.Year && monthMark.Month == endDate.Year)
        //                    break;

        //                graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
        //                    + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));
        //                monthMark = monthMark.AddMonths(2);
        //            }

        //            //Mark end 
        //            graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));
        //        }
        //        else if (totalMonths >= 8)
        //        {
        //            //TODO: Mark start (nl), months at the interval rate (totalMonths % 4), and the end (nl)
        //            //Mark start
        //            graph.xAxisMarkers.Add(new AxisMarker("", startDate.Ticks, AxisMarker.TickTypes.None));

        //            //The month marks
        //            DateTime monthMark = new DateTime(startDate.Year, startDate.Month, 1);
        //            int intervalRate = (int)Math.Floor(totalMonths / 4);
        //            monthMark = monthMark.AddMonths(1);
        //            while (monthMark < endDate)
        //            {
        //                //Another way to break out
        //                if (monthMark.Year == endDate.Year && monthMark.Month == endDate.Year)
        //                    break;

        //                graph.xAxisMarkers.Add(new AxisMarker(monthMark.Month.ToString("d", CultureInfo.CurrentCulture)
        //                    + "/" + monthMark.Year, monthMark.Ticks, AxisMarker.TickTypes.Large));
        //                monthMark = monthMark.AddMonths(intervalRate);
        //            }

        //            //Mark end 
        //            graph.xAxisMarkers.Add(new AxisMarker("", endDate.Ticks, AxisMarker.TickTypes.None));
        //        }


        //        //Y-Axis labels and values
        //        var orderedValues = _stats.CostMonthlyTotal.OrderBy((dv) => dv.Value);
        //        Double startValue = orderedValues.First().Value;
        //        Double endValue = orderedValues.Last().Value;
        //        Double middleValue = startValue + (endValue - startValue) / 2;
        //        Double buffer = (endValue - startValue) * .35;

        //        String startValueLabel = startValue.ToString("c", CultureInfo.CurrentCulture);
        //        String middleValueLabel = middleValue.ToString("c", CultureInfo.CurrentCulture);
        //        String endValueLabel = endValue.ToString("c", CultureInfo.CurrentCulture);

        //        graph.yAxisMarkers.Add(new AxisMarker("", startValue - buffer, AxisMarker.TickTypes.None));
        //        graph.yAxisMarkers.Add(new AxisMarker(startValueLabel, startValue, AxisMarker.TickTypes.Large));
        //        graph.yAxisMarkers.Add(new AxisMarker(middleValueLabel, middleValue, AxisMarker.TickTypes.Large));
        //        graph.yAxisMarkers.Add(new AxisMarker(endValueLabel, endValue, AxisMarker.TickTypes.Large));
        //        graph.yAxisMarkers.Add(new AxisMarker("", endValue + buffer, AxisMarker.TickTypes.None));

        //        //Add data points
        //        foreach (DateValue<Double> dateValue in _stats.CostMonthlyTotal)
        //        {
        //            graph.Data.Add(new OrderedPair<Double, Double>(dateValue.Date.Ticks, dateValue.Value));
        //        }
        //    }

        //    GraphControl graphControl = new GraphControl(graph, LanguageResources.Resources._cost);
        //    graphControl.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
        //    graphControl.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;



        //    this.costGrid.Children.Add(graphControl);
        //}

    }
}